window.onload = function () {

    const {createEditor, createToolbar} = window.wangEditor

    const editorConfig = {
        placeholder: 'Type here...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('editor content', html)
            // 也可以同步到 <textarea>
        },
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default', // or 'simple'
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    })

    $("#submit-btn").click(function (event) {
        event.preventDefault();
        let title = $("#exampleFormControlInput1").val();
        let content = editor.getHtml();
        let category = $("#category-select").val();
        let csrfmiddlewaretoken = $('input[name="csrfmiddlewaretoken"]').val();
        $.ajax('pub_blogs', {
            method: 'POST',
            data: {
                title: title,
                content: content,
                category: category,
                csrfmiddlewaretoken: csrfmiddlewaretoken
            },
            success: function (response) {
                if (response['code'] == 200) {
                    alert(response.message);
                    let blog_id = response['data']['blog_id'];
                    window.location = 'detail/' + blog_id;
                } else {
                    alert(response.message);
                }

            },


        })
    });

}